
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.UUID;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Patrick
 */
public class VersusManager {

    public static ArrayList<Versus> getList(Connection connection) throws SQLException {

        ArrayList<Versus> list = new ArrayList();
        String sql = "SELECT id,leftlabel,rightlabel,leftcounter,rightcounter,enddate,bgimage from versus order by enddate desc limit 100";
        Statement stat = connection.createStatement();
        ResultSet rs = stat.executeQuery(sql);
        while (rs.next()) {
            Versus v = new Versus();
            v.setId(rs.getInt("id"));
            v.setLeftSide(rs.getString("leftlabel"));
            v.setRightSide(rs.getString("rightlabel"));
            v.setTotalVotes(rs.getInt("leftcounter") + rs.getInt("rightcounter"));
            v.setResult(rs.getInt("leftcounter") - rs.getInt("rightcounter"));
            v.setBgImage(rs.getString("bgimage"));
            int remaining = (int) ((rs.getDate("enddate").getTime() - System.currentTimeMillis()) / 1000 / 60 / 60);
            if (remaining < 0) {
                remaining = 0;
            }
            v.setRemainingHours(remaining);
            list.add(v);
        }
        rs.close();
        stat.close();
        return list;
    }

    public static void voteLeft(Connection connection, int id) throws SQLException {
        String sql = "UPDATE versus set leftcounter = (leftcounter+1) where id=" + id;
        Statement stat = connection.createStatement();
        stat.execute(sql);
        stat.close();
    }

    public static void voteRight(Connection connection, int id) throws SQLException {
        String sql = "UPDATE versus set rightcounter = (rightcounter+1) where id=" + id;
        Statement stat = connection.createStatement();
        stat.execute(sql);
        stat.close();
    }

    public static String getXMLVersusList(Connection con) throws ParserConfigurationException, TransformerConfigurationException, SQLException, TransformerException {
        Document doc = getBaseDocument();
        Node root = doc.getElementsByTagName("crombz").item(0);
        ArrayList<Versus> list = getList(con);
        String uuid = UUID.randomUUID().toString();
        for (Versus v : list) {
            Node versus = doc.createElement("versus");
            Node left = doc.createAttribute("left");
            Node right = doc.createAttribute("right");
            Node totalvotes = doc.createAttribute("totalvotes");
            Node result = doc.createAttribute("result");
            Node remaining = doc.createAttribute("remaininghours");
            Node id = doc.createAttribute("id");
            Node bgImage = doc.createAttribute("bgimage");
            id.setNodeValue(v.getId() + "");
            left.setNodeValue(v.getLeftSide());
            right.setNodeValue(v.getRightSide());
            totalvotes.setNodeValue(v.getTotalVotes() + "");
            result.setNodeValue(v.getResult() + "");
            remaining.setNodeValue(v.getRemainingHours() + "");
            bgImage.setNodeValue(v.getBgImage());
            versus.getAttributes().setNamedItem(left);
            versus.getAttributes().setNamedItem(right);
            versus.getAttributes().setNamedItem(totalvotes);
            versus.getAttributes().setNamedItem(result);
            versus.getAttributes().setNamedItem(remaining);
            versus.getAttributes().setNamedItem(id);
            versus.getAttributes().setNamedItem(bgImage);
            root.appendChild(versus);
        }
        return getXML(doc);
    }

    private static Document getBaseDocument() throws ParserConfigurationException {
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        doc.appendChild(doc.createElement("crombz"));
        return doc;
    }

    private static String getXML(Document doc) throws TransformerConfigurationException, TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.getBuffer().toString().replaceAll("\n|\r", "");
    }
}
